$(function () {
    //抽取变量
    var sche_form = $("#sche_form");
    var sche_dialog = $("#sche_dialog");
    var sche_datagrid = $("#sche_datagrid");
    var task_dialog = $("#task_dialog");
    var task_form = $("#task_form");
    var btn_changeState = $("#btn_changeState");
    //初始化datagrid
    sche_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        striped: true,
        url: "/task/query.do",
        pagination: true,
        singleSelect: true,
        toolbar: "#tb",
        columns: [[
            {field: 'date', title: '日期', width: 100, align: 'center'},
            {field: 'emp', title: '姓名', width: 100, align: 'center',formatter:function(value, row, index) {
                    return value ? value.username : "";
                }},
            {field: 'dept', title: '部门', width: 100, align: 'center',formatter:function(value, row, index) {
                    return value ? value.name : "";
                }},
            {field: 'sn', title: '任务', width: 100, align: 'center'},
            {field: 'state', title: '状态', width: 100, align: 'center', formatter: function (value, row, index) {
                    if (value == null) {
                        return "<font color='red'>未完成</font>";
                    } else {
                        return value ? "<font color='green'>已完成</font>" : "<font color='orange'>待完成</font>";
                    }
                }},

        ]],
        //禁用按钮或启用按钮
        onClickRow: function (index, row) {
            if (row.state == 1) {
                $("#state_btn").linkbutton('disable',true);
                $("#state_btn1").linkbutton('disable',true);
            } else if(row.state == 0){
                $("#state_btn1").linkbutton('enable',true);
                $("#state_btn").linkbutton('enable',true);
            }
        }
    });
    //初始化dialog
    sche_dialog.dialog({
        width: 400,
        height: 450,
        buttons: "#sche_btns",
        closed: true,
        onClose: function () {
            sche_form.form("clear");
        }
    });
    //初始化查看任务的dialog
    task_dialog.dialog({
        width: 400,
        height: 400,
        resizable: true,
        buttons: "#task_btns",
        draggable:true,
        closed: true,
        onClose: function () {
            task_form.form("clear");
        }
    });
    //所有a标签的方法都交给methodObject对象来管理
    var methodObj = {
        add: function () {
            sche_form.form("clear");
            sche_dialog.dialog("setTitle", "新建任务单");
            sche_dialog.dialog("open");
        },
        //编辑
        edit: function () {
            var row = sche_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }

            if(row.state != 1){
                //处理部门数据
                row["emp.id"] = row.emp.id;
                row["dept.id"] = row.dept.id;
                sche_form.form("load", row);
                sche_dialog.dialog("setTitle", "编辑");
                sche_dialog.dialog("open");
            }
        },
        //查看任务明细
        look: function () {
            var row = sche_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中查看的数据', 'info');
                return;
            }
            //处理部门数据
            row["emp.id"] = row.emp.id;
            row["dept.id"] = row.dept.id;
            task_form.form("load", row);
            task_dialog.dialog("setTitle", "查看任务明细")
            task_dialog.dialog("open");
        },
        //保存
        save: function () {
            sche_form.form("submit", {
                url: "/task/saveOrUpdate.do",
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            sche_dialog.dialog("close");
                            sche_datagrid.datagrid("load");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                    }
                }
            })
        },
        //取消
        cancel: function () {
            sche_dialog.dialog("close");
        },
        //改变任务完成的状态
        changeState: function () {
            var schedule = sche_datagrid.datagrid("getSelected");
            if (!schedule) {
                $.messager.alert("温馨提示", "亲！请选中一条数据哦！", "info");
                return;
            }else{
                var flag = schedule.state ? "待完成" : "已完成";
                var state = schedule.state ? "0" : "1";
                if(schedule.state != 1) {
                    $.messager.confirm("确认窗口", "确认要标记" + flag + "么?", function (r) {
                        if (r) {
                            var id = sche_datagrid.datagrid("getSelected").id;
                            $.get("/task/changeState.do", {id: id}, function (data) {
                                if (data.success) {
                                    $.messager.alert("温馨提示", data.msg, "info", function () {
                                        sche_datagrid.datagrid("reload");
                                    })
                                } else {
                                    $.messager.alert("温馨提示", date.msg, "error");
                                }
                            })
                        }
                    })
                }
            }

        },

        //高级查询
        searchForm:function() {
            var keyword = $("input[name='keyword']").val();
            var deptId = $(".dept_combobox").combobox('getValue');
            var beginDate = $("input[name='beginDate']").val();
            var endDate = $("input[name='endDate']").val();

            sche_datagrid.datagrid("load", {
                keyword: keyword,
                deptId: deptId,
                beginDate: beginDate,
                endDate: endDate
            });
        },
        //打印任务
        exportXls: function () {
            window.location.href = "/task/exportXls.do";
        },
        //刷新
        reload: function () {
            sche_datagrid.datagrid("load");
        }
    };
    //所有a标签的点击事件对应的方法都交给methodObject对象来管理,调用的时候只需要获取对应的cmd质量,然后通过method对象来调用即可
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});

